package zhihui.mobile.ui.shop.address;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.zgm.utils.UiUtils;
import com.zgm.utils.ui.UiTextHeader;

import java.util.List;

import zhihui.mobile.R;
import zhihui.mobile.bean.address.Province;
import zhihui.mobile.bean.shop.address.ShoppingAddress;
import zhihui.mobile.presenter.IPresenter;
import zhihui.mobile.presenter.shop.ShopAddressAddPresenter;
import zhihui.mobile.ui.BaseActivity;

/**
 * Created by 张公铭 on 2017/8/16 0016.
 */

public class ShopAddressAddActivity extends BaseActivity implements IShopAddressAddContract.IShopAddressAddView
{
    private IShopAddressAddContract.IShopAddressAddPresenter addPresenter = new ShopAddressAddPresenter();
    private UiTextHeader mheader;
    private EditText ed_name;
    private EditText ed_phone;
    private TextView tv_area;
    private EditText ed_address;


    @Override
    public void initView()
    {
        mheader = (UiTextHeader) findViewById(R.id.shop_address_add_header);
        mheader.setTitle("添加收货地址");
        mheader.setLeftBottonIcon(R.drawable.shop_back);
        mheader.setLeftButtonOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                goBack(ShopAddressAddActivity.class);
            }
        });
        mheader.setRightText("保存");
        mheader.setRightTextListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (TextUtils.isEmpty(ed_name.getText()))
                {
                    showMessage("请输入收货人姓名！");
                    UiUtils.editTextFocus(ed_name);
                    return;
                }
                if (TextUtils.isEmpty(ed_phone.getText()))
                {
                    showMessage("请输入收货人手机号！");
                    UiUtils.editTextFocus(ed_phone);
                    return;
                }
                if (ed_phone.getText().toString().length()<11)
                {
                    showMessage("请输入正确的手机号！");
                    UiUtils.editTextFocus(ed_phone);
                    return;
                }
                if (TextUtils.isEmpty(tv_area.getText()))
                {
                    showMessage("请选择所在地区");
                    return;
                }
                if (TextUtils.isEmpty(ed_address.getText()))
                {
                    showMessage("请输入详细地址！");
                    UiUtils.editTextFocus(ed_address);
                    return;
                }
                addPresenter.addAddress(ed_name.getText().toString(), ed_phone.getText().toString(), ed_address.getText().toString());
            }
        });

        ed_name = (EditText) findViewById(R.id.et_shop_address_add_name);
        UiUtils.editTextFocus(ed_name);

        ed_phone = (EditText) findViewById(R.id.et_shop_address_add_phone);
        UiUtils.editTextFocus(ed_phone);
        tv_area = (TextView) findViewById(R.id.tv_shop_address_add_area);
        tv_area.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                addPresenter.showAddressDialog();
            }
        });
        ed_address = (EditText) findViewById(R.id.et_shop_address_add_address);
        UiUtils.editTextFocus(ed_address);
        UiUtils.editTextFocus(ed_name);
    }

    @Override
    protected void parseArgumentsFormIntent(Intent argIntent)
    {
        int addressId = argIntent.getIntExtra("update_address_id", 0);
        addPresenter.setUpdateId(addressId);
    }

    @Override
    protected int getLayoutResId()
    {
        return R.layout.shop_address_add_activity;
    }

    @Override
    protected IPresenter[] getPresenters()
    {
        return new IPresenter[]{addPresenter};
    }

    @Override
    protected void onInitPresenter()
    {
        addPresenter.init(this, this);
    }

    @Override
    public void showAddressDialog(List<Province> list)
    {
        tv_area.setText("");
        zhihui.mobile.ui.utils.UiUtils.showAddressDialog(this, list, new zhihui.mobile.ui.utils.UiUtils.IAddressSelectListener()
        {
            @Override
            public void updateText(String name, int type)
            {
                switch (type)
                {
                    case 1:
                        tv_area.setText(name);
                        break;
                    case 2:
                        String values = tv_area.getText().toString();
                        values += "  " + name;
                        tv_area.setText(values);
                        break;
                    case 3:
                        String area = tv_area.getText().toString();
                        area += "  " + name;
                        tv_area.setText(area);
                        break;
                }
            }

            @Override
            public void setProv(String name, String code)
            {
                addPresenter.setProv(name, code);
            }

            @Override
            public void setCity(String name, String code)
            {
                addPresenter.setCity(name, code);
            }

            @Override
            public void setArea(String name, String code)
            {
                addPresenter.setArea(name, code);
            }
        });
    }

    @Override
    public void initData(ShoppingAddress address)
    {
        mheader.setTitle("修改地址");
        ed_name.setText(address.getName());
        ed_phone.setText(address.getTel());
        tv_area.setText(address.getProv() + "  " + address.getCity() + "  " + address.getDist());
        ed_address.setText(address.getDetails());
        UiUtils.editTextFocus(ed_name);
    }
}
